/* 	IRSA Study 
	Dizon, Gong, Jones
	Updated: Jan 5 2018
	Generates savings results for IRSA study
*/

** Preliminaries **
clear all
macro drop _all

** Paths **
global path "/Users/egong/Dropbox/iSAVE/Outputs/Papers/savings_irsa/submission-JHR/publication/Replication Package"

global data "$path/DATA"
global output "$path/OUTPUT"
 
** Output to Table **
global RESULTS "Table3.xls"
capture erase $RESULTS 


**************************************************
* Table 3: Effects of intervention on savings	 *
**************************************************
set more off

	** Panel A: Administrative Data **
cd "$data"
use "irsa_admin_panel.dta", clear
xtset rid wave_m_b
global rounds "mid post"

** Weekly Deposits in Labeled, Exisiting, Combined Accounts **
	** Labeled Account **
xtreg mpesa2_depipa $rounds if i_treat==1, fe vce(cluster rid)	
	estimate store mpesa2depipa

foreach x in 1 all {
	xtreg mpesa`x'_depipa  	i_treat_post  i_treat_mid $rounds, fe vce(cluster rid)	
	estimate store mpesa`x'depipa
	mean mpesa`x'_depipa if i_treat==0 & post==1
	matrix b=e(b)
	estadd scalar control_mean = b[1,1]: mpesa`x'depipa

	}

** Weekly Balances in Labeled, Exisiting, and Combined Accounts **	
xtreg wb_m_dailybal2 $rounds if i_treat==1, fe vce(cluster rid)	
	estimate store mpesa2bal

foreach x in  1  {
	xtreg wb_m_dailybal`x'  	i_treat_post i_treat_mid  $rounds, fe vce(cluster rid)	
	estimate store mpesa`x'bal
	mean wb_m_dailybal`x' if i_treat==0 & post==1
	matrix b=e(b)
	estadd scalar control_mean = b[1,1]: mpesa`x'bal
}
	xtreg svmobileAD  	i_treat_post i_treat_mid $rounds, fe vce(cluster rid)	
	estimate store mpesaallbal
	mean svmobileAD if i_treat==0 & post==1
	matrix b=e(b)
	estadd scalar control_mean = b[1,1]: mpesaallbal

** Results **
cd "$output"
#delimit;
	xml_tab mpesa2depipa mpesa1depipa mpesaalldepipa mpesa2bal mpesa1bal mpesaallbal
	using "$RESULTS", replace
	keep ( i_treat_post post)
	below
	stats( N control_mean)
	title ("Admin Accounts")
	format ( (SCCR0) (SCCR0 NBCR0) (SCCR0 NBCR0) (SCCR0 NBCR0) (SCCR0 NBCR0) (SCCR0 NBCR2) (SCCR0 NBCR0)) 
	sheet("admin");
#delimit cr
	
	
	** Panel B: Self Reported Data **
	** Use only baseline and endline surveys **

cd "$data"	
use "irsa_selfreportsavings.dta", clear

global accounts "mbank svown svbank"

** Self Reported Balances **
set more off
foreach y in $accounts  {
reg `y'_w i_treat i_treat_post $controls post, vce(cluster rid)
	estimates store `y'_w
	mean `y'_w if i_treat==0 & post==1
	matrix b = e(b)
	estadd scalar control_mean = b[1,1]: `y'_w
	
	
}

reg numroscas i_treat i_treat_post $controls post , robust
	estimates store numroscas
	mean numroscas if i_treat==0 & post==1
	matrix b = e(b)
	estadd scalar control_mean = b[1,1]: numroscas
	
** Results **
cd "$output"
#delimit;
	xml_tab mbank* svown* svbank* numroscas
	using "$RESULTS", append
	keep ( i_treat_post post)
	below
	stats( N control_mean)
	title ("Self-Reported Accounts")
	format ( (SCCR0) (SCCR0 NBCR0) (SCCR0 NBCR0) (SCCR0 NBCR0) (SCCR0 NBCR2)) 
	sheet("self_reported");
#delimit cr



